var model = require('../model/model.js');

exports.list = function(req, res){
  res.statusCode = 200;
  model.discussions().list(
  	function(results)
  	{
  		res.send(results);
  	});
};

exports.get = function(req, res, next) {
    var id = req.params.id;
    var statusCode = 200;
    var result;
    
    if (id) 
    {
    	console.log('api::get: ' + id);
    	model.discussions().get(id,
    		function(result)
    		{ 
    			if (!result)
    			{
    				statusCode = 404;
    				result = {"id":2, "message":"Discussion does not exist."};
    			}
    			res.statusCode = statusCode;
    			res.send(result);
    		});
    }
    else 
    {
    	next();
    }
}

exports.add = function(req, res) {
    console.log("req.body: " + JSON.stringify(req.body));
    var discussion = req.body;
    
    if (discussion)
    {
    	console.log('api::add: ' + JSON.stringify(discussion));
		model.discussions().add(discussion,
			function(added)
			{
		    	console.log('api::added: ' + JSON.stringify(added));
				res.statusCode = 200;
				res.send(added);
			});	
    }
	else
	{
	    res.statusCode = 406;
		res.send({ id : 1, message : 'Cannot add.  Nothing was sent.' });
	}
};

exports.delete = function(req, res, next) {
    var id = req.params.id;
    var statusCode = 200;
    var result;

    if (id)
    {
        console.log('api::delete: ' + id);
        model.discussions().delete(id,
            function(result)
            {
                if (!result)
                {
                    statusCode = 404;
                    result = {"id":2, "message":"Discussion does not exist."};
                }
                res.statusCode = statusCode;
                res.send(result);
            });
    }
    else
    {
        next();
    }
}

